package net.awpspace.mobile.management.house;

import java.util.Vector;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.form.BaseForm;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Font;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;

public class DeleteMemberViewer extends BaseForm {

	private Vector list;

	private Vector vector = new Vector();

	private String SELETALL = "Select All";
	private String UNSELECTALL = "Unselect All";
	private String DELETE = "Delete";
	
	private Command selectAll;
	private Command delete;
	private Command unSelectAll;

	public DeleteMemberViewer(MIDlet app, BaseForm parent) {
		super(app, parent);
		// TODO Auto-generated constructor stub
		setTitle("Delete Member");
		setTransitionInAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		createForm();
		createCommand();
		Style s;
		s = new Style();
		Font font = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_SMALL);
		s.setFont(font);
		UIManager.getInstance().setComponentStyle("List", s);
	}

	public void createCommand() {
		selectAll = new Command(SELETALL);
		unSelectAll = new Command(UNSELECTALL);
		delete = new Command(DELETE);
		addCommand(unSelectAll);
		addCommand(selectAll);
		addCommand(delete);
	}

	private void createForm() {

		list = Persistence.getInstance().getData();
		for (int i = 0; i < list.size(); i++) {
			CheckBox cb = new CheckBox(((HouseMember)list.elementAt(i)).getName());
			vector.addElement(cb);
			addComponent(cb);
		}

	}

	public void delete() {
		System.out.println("Size: " + list.size());
		for(int i = 0; i < vector.size(); i++){
			CheckBox cb = ((CheckBox)vector.elementAt(i));
			System.out.println("Selected: " + cb.isSelected());
			if(cb.isSelected()){				
				removeComponent(cb);
				vector.removeElement(cb);
				Persistence.getInstance().delete(i);
				System.out.println("Delete: " + i);				
//				refreshTheme();				
				i--;
			}
		}		
	}

	public void selectAll() {
		for(int i = 0; i < vector.size(); i++){
			((CheckBox)vector.elementAt(i)).setSelected(true);
			System.out.println(i + ((HouseMember)list.elementAt(i)).getName());
		}
	}
	public void unSelectAll(){
		for(int i = 0; i < vector.size(); i++){
			((CheckBox)vector.elementAt(i)).setSelected(false);
		}
	}

	protected void processCommand(ActionEvent e) {
		// TODO Auto-generated method stub
		AudioManager.getInstance().play(AudioManager.BUTTON);
		if (e.getCommand().equals(delete)) {
			delete();
		} else if (e.getCommand().equals(selectAll)) {
			selectAll();
		}
		else if (e.getCommand().equals(unSelectAll)) {
			unSelectAll();
		}
	}

}
